package com.junmeng.libfloating.base

import android.os.Handler
import android.os.Looper

/**
 * 只能存在一条延时任务
 */
class SingleDelayMainHandler : Runnable {

    var mMainHandler: Handler = Handler(Looper.getMainLooper())

    var mTask: Runnable? = null

    /**
     * 终止上次的任务，并延时开启本次任务
     * @param run Runnable
     * @param delayMs Long
     */
    @Synchronized
    fun postDelayed(run: Runnable, delayMs: Long) {
        this.mTask = run
        mMainHandler.removeCallbacks(this)
        mMainHandler.postDelayed(this, delayMs)
    }

    fun stop() {
        mMainHandler.removeCallbacks(this)
    }

    override fun run() {
        this.mTask?.run()
    }
}